﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;
using System.Data;

namespace LogicUniversity.Employee
{
    public partial class MakeRequest : System.Web.UI.Page
    {
        private MakeRequestControl requestControl;
<<<<<<< .mine
        List<Requisition_Detail> selectedItemList;
        private EmailController mailControl;

        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //if (Session["Role"] as string != "Employee" || Session["Role"] as string != "Representative")
            //{
            //    
            //}
            if (Session["Role"] as string != "Employee")
            {
                if (Session["Role"] as string == "Representative")
                {
                    this.MasterPageFile = "~/Representative/RepMaster.Master";
                }
                else
                {
                    Response.Redirect("~/Login.aspx");
                }
            }
            
        }
        private void putNotification(LogicUniversityBiz.Employee emp)
        {
            
            noti.Value = "Delegated";
            lblName.Text = emp.Employee_Name;

            btnGo.Visible = true;
        }
=======
        List<Requisition_Detail> selectedItemList;

>>>>>>> .r28
        protected void Page_Load(object sender, EventArgs e)
        {
<<<<<<< .mine
            
            if (!IsPostBack)
            {
               
                requestControl = new MakeRequestControl();

                string empID = Session["UserID"] as string;
                //string empName = requestControl.getEmpName(empID);
                LogicUniversityBiz.Employee emp = requestControl.getEmployeeInfo(empID);

                if (emp.Temp_Role == "Department Head")
                {
                    noti_panel.Visible = true;
                    putNotification(emp);
                }
                else
                {
                    noti_panel.Visible = false;
                }

                List<string> categoryList = requestControl.getCategory();

                categoryList.Insert(0, "All");
                ddlCategory.DataSource = categoryList;
                ddlCategory.DataBind();

                List<Catelogue> catelogueList = requestControl.getAllCatelogueItems();
                bindCatelogueView(catelogueList);

                SetInitialSelectedItemView();
            }
            else
            {
                add.Enabled = true;
            }
            

        }

        private void bindCatelogueView(List<Catelogue> catList)
        {
            CatelogueView.DataSource = catList;
            CatelogueView.DataBind();
        }

        private void bindSelectedItemView()
        {
            selectedItemView.DataSource = selectedItemList;
            selectedItemView.DataBind();
        }

        private void SetInitialSelectedItemView()
        {
            DataTable dt = new DataTable();
            //DataRow row = null;

            dt.Columns.Add(new DataColumn("code",typeof(string)));//for item code
            dt.Columns.Add(new DataColumn("desc",typeof(string)));//for desc
            dt.Columns.Add(new DataColumn("qty",typeof(string)));//for qty textbox
            dt.Columns.Add(new DataColumn("remove",typeof(string)));//for remove link

            //row = dt.NewRow();
            //dt.Rows.Add(row);

            //selectedItemView.DataSource = dt;
            //selectedItemView.DataBind();

            //((TextBox)selectedItemView.Rows[0].Cells[2].FindControl("selectedQty")).Text = "New Row";
            ViewState["SelectedItems"] = dt;
        }
         
   

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
=======
            if (!IsPostBack)
            {

                requestControl = new MakeRequestControl();

                List<string> categoryList = requestControl.getCategory();


                ddlCategory.DataSource = categoryList;
                ddlCategory.DataBind();

                List<Catelogue> catelogueList = requestControl.getAllCatelogueItems();
                bindCatelogueView(catelogueList);

                SetInitialSelectedItemView();
            }
            

        }

        private void bindCatelogueView(List<Catelogue> catList)
        {
            CatelogueView.DataSource = catList;
            CatelogueView.DataBind();
        }

        private void bindSelectedItemView()
        {
            selectedItemView.DataSource = selectedItemList;
            selectedItemView.DataBind();
        }

        private void SetInitialSelectedItemView()
        {
            DataTable dt = new DataTable();
            //DataRow row = null;

            dt.Columns.Add(new DataColumn("code",typeof(string)));//for item code
            dt.Columns.Add(new DataColumn("desc",typeof(string)));//for desc
            dt.Columns.Add(new DataColumn("qty",typeof(string)));//for qty textbox
            dt.Columns.Add(new DataColumn("remove",typeof(string)));//for remove link

            //row = dt.NewRow();
            //dt.Rows.Add(row);

            //selectedItemView.DataSource = dt;
            //selectedItemView.DataBind();

            //((TextBox)selectedItemView.Rows[0].Cells[2].FindControl("selectedQty")).Text = "New Row";
            ViewState["SelectedItems"] = dt;
        }
         
        protected void btnSelect_Click(object sender, EventArgs e)
        {
>>>>>>> .r28
            requestControl = new MakeRequestControl();
<<<<<<< .mine
            string itemCode;
            int qty=0;
=======
            String selectedCategory = ddlCategory.Items[ddlCategory.SelectedIndex].Value.ToString();
          
>>>>>>> .r28

<<<<<<< .mine
            //to retrieve from Session later
            string employeeID = Session["UserID"] as string;
=======
            List<Catelogue> catelogueList = requestControl.getCatelogueItem(selectedCategory.Trim());
            bindCatelogueView(catelogueList);
        }
>>>>>>> .r28

<<<<<<< .mine
            Requisition requisition;
            Requisition_Detail reqDetail;
           
            DateTime requestedDate = System.DateTime.Now;
            string processStatus = "Pending";
            string approvedStatus = "Pending";
            string deptCode = requestControl.getDepartmentCodeOf(employeeID);
=======
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            requestControl = new MakeRequestControl();
            string itemCode;
            int qty;
>>>>>>> .r28

<<<<<<< .mine
            requisition = new Requisition();
            requisition.Requested_Date = requestedDate;
            requisition.Process_Status = processStatus;
            requisition.Status = approvedStatus;
            requisition.Emp_ID = employeeID;
            requisition.Dept_Code = deptCode;

            int reqID=requestControl.recordRequisition(requisition);

            foreach (GridViewRow row in selectedItemView.Rows)
            {
                itemCode=((HiddenField)row.Cells[0].FindControl("selectedItemCode")).Value.Trim();

                try
                {
                    qty = Convert.ToInt32(((TextBox)row.Cells[2].FindControl("selectedQty")).Text.Trim());
                }
                catch (FormatException fe)
                {
                    ((Label)row.Cells[2].FindControl("lblerror")).Text = "Illegal Quantity";
                    return;
                }

                if (qty > 0)
                {
                    ((Label)row.Cells[2].FindControl("lblerror")).Text =null;
                    reqDetail = new Requisition_Detail();
                    reqDetail.Item_Code = itemCode;
                    reqDetail.Quantity = qty;
                    reqDetail.Req_ID = reqID;

                    requestControl.recordRequisiitonDetail(reqDetail);
                }
                else
                {
                    ((Label)row.Cells[2].FindControl("lblerror")).Text = "Illegal Quantity";
                    return;
                }
            }

            mailControl = new EmailController();
           // mailControl.FromEmail ="h.waeyan@gmail.com"; //requestControl.getFromEmail(employeeID);
            //mailControl.ToEmail = "jojokyi21@gmail.com";//requestControl.getHeadEmail(deptCode);
            //mailControl.Subject = "Stationery Requisition";
            //mailControl.Body = "blblblbl";
            string hostname = Dns.GetHostName();
            IPAddress[] addressLst = Dns.GetHostAddresses(hostname);
            string ipAdd = addressLst[1].ToString();
            StringBuilder strb = new StringBuilder();
            strb.Append("Stationery Request");
            strb.Append("This is the message to approve the requestion make by "+requestControl.getEmpName(employeeID)+" . You can check from this link " + ipAdd + "/LogicUniversity/Department_Head/RequestsDetail.aspx?reqID="+reqID+"click here!");
            strb.Append("");
            mailControl.send(requestControl.getHeadEmail(deptCode), requestControl.getFromEmail(employeeID), "Stationery Requisition", strb.ToString() );

            Response.Redirect("MakeRequestDetail.aspx?reqID="+reqID);
=======
            //to retrieve from Session later
            string employeeID = "A0108090";

            Requisition requisition;
            Requisition_Detail reqDetail;
           
            DateTime requestedDate = System.DateTime.Today;
            string processStatus = "Pending";
            string approvedStatus = "Pending";
            string deptCode = requestControl.getDepartmentCodeOf(employeeID);

            requisition = new Requisition();
            requisition.Requested_Date = requestedDate;
            requisition.Process_Status = processStatus;
            requisition.Status = approvedStatus;
            requisition.Emp_ID = employeeID;
            requisition.Dept_Code = deptCode;

            int reqID=requestControl.recordRequisition(requisition);

            foreach (GridViewRow row in selectedItemView.Rows)
            {
                itemCode=((HiddenField)row.Cells[0].FindControl("selectedItemCode")).Value.Trim();
                qty = Convert.ToInt32(((TextBox)row.Cells[2].FindControl("selectedQty")).Text.Trim());

                reqDetail = new Requisition_Detail();
                reqDetail.Item_Code = itemCode;
                reqDetail.Quantity = qty;
                reqDetail.Req_ID = reqID;

                requestControl.recordRequisiitonDetail(reqDetail);
            }

            Response.Redirect("MakeRequestDetail.aspx?reqID="+reqID);
>>>>>>> .r28
        }

<<<<<<< .mine
       


        protected void add_Click(object sender, EventArgs e)
        {
            

            if (ViewState["SelectedItems"] != null)
            {
                DataTable table = (DataTable)ViewState["SelectedItems"];
                DataRow row = null;

                foreach (GridViewRow gridRow in CatelogueView.Rows)
                {
                    CheckBox chk = (CheckBox)gridRow.Cells[4].FindControl("chkChoose");

                    int rowIndex = 0;
                    if (chk.Checked)
                    {
                        ((TextBox)gridRow.Cells[3].FindControl("txtQty")).Enabled = true;
                        string itemCode = ((HiddenField)gridRow.Cells[0].FindControl("itemCode")).Value;
                        string desc = gridRow.Cells[1].Text;
                        int qty=0;
                        try
                        {
                            qty = Convert.ToInt32(((TextBox)gridRow.Cells[3].FindControl("txtQty")).Text.Trim());

                            if (qty <= 0)
                            {
                                ((Label)gridRow.Cells[3].FindControl("lblmsg")).Text = "Illegal Quantity";
                                return;
                            }
                        }
                        catch (FormatException fe)
                        {
                            ((Label)gridRow.Cells[3].FindControl("lblmsg")).Text = "Illegal Quantity";
                            return;
                        }

                        ((Label)gridRow.Cells[3].FindControl("lblmsg")).Text = null;

                        if (table.Rows.Count > 0)//if there're rows, check if rows in the selectedItem view are matched.
                        {
                            bool isFound = false;
                            foreach (GridViewRow selectedItemRow in selectedItemView.Rows)
                            {
                                string selectedItemCode = ((HiddenField)selectedItemRow.Cells[0].FindControl("selectedItemCode")).Value.Trim();
                                if (itemCode == selectedItemCode)
                                {
                                    TextBox currentQtyBox = (TextBox)selectedItemRow.Cells[2].FindControl("selectedQty");
                                    int currentQty = Convert.ToInt32(currentQtyBox.Text.Trim());

                                    currentQtyBox.Text = (currentQty + qty).ToString();
                                    table.Rows[rowIndex]["qty"] = (currentQty + qty).ToString();
                                    ViewState["SelectedItems"] = table;

                                    isFound = true;
                                    break;
                                }
                                else
                                {
                                    rowIndex++;
                                    isFound = false;
                                }
                            }//end of selected item view
                            if (!isFound)
                            {
                                row = table.NewRow();
                                row["code"] = itemCode;
                                row["desc"] = desc;
                                row["qty"] = qty.ToString();

                                table.Rows.Add(row);
                            }

                        }
                        else//if no row, add new row
                        {
                            row = table.NewRow();
                            row["code"] = itemCode;
                            row["desc"] = desc;
                            row["qty"] = qty.ToString();

                            table.Rows.Add(row);
                        }
                    }
                    else
                    {
                        ((TextBox)gridRow.Cells[3].FindControl("txtQty")).Enabled = false;
                    }
                }//end of catelogue view looping
                selectedItemView.DataSource = table;
                selectedItemView.DataBind();
            }


            setDataToSelectedItemGridViewControls();
            btnSubmit.Visible = true;
        }

        private void setDataToSelectedItemGridViewControls()
        {
            int rowIndex = 0;
            if (ViewState["SelectedItems"] != null)
            {
                DataTable table = (DataTable)ViewState["SelectedItems"];
                if (table.Rows.Count > 0)
                {
                    foreach(GridViewRow row in selectedItemView.Rows)
                    {
                        HiddenField newItemCode = (HiddenField)row.Cells[0].FindControl("selectedItemCode");
                        Label newItemDesc = (Label)row.Cells[1].FindControl("selectedItemDesc");
                        TextBox newItemQtyBox = (TextBox)row.Cells[2].FindControl("selectedQty");

                        newItemCode.Value = table.Rows[rowIndex]["code"].ToString();
                        newItemDesc.Text = table.Rows[rowIndex]["desc"].ToString();
                        newItemQtyBox.Text = table.Rows[rowIndex]["qty"].ToString();

                        ++rowIndex;
                    }
                }
            }
        }

        protected void removeLink_Click(object sender, EventArgs e)
        {
            LinkButton removeBtn = (LinkButton)sender;
            GridViewRow clickedRow = (GridViewRow)removeBtn.NamingContainer;

            int rowIndex = clickedRow.RowIndex;
            DataTable table = (DataTable)ViewState["SelectedItems"];

            table.Rows[rowIndex].Delete();
            ViewState["SelectedItems"] = table;

            if (table == null || table.Rows.Count <= 0)
            {
                btnSubmit.Visible = false;
            }
            selectedItemView.DataSource = table;
            selectedItemView.DataBind();

            setDataToSelectedItemGridViewControls();
            
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            requestControl = new MakeRequestControl();
            String selectedCategory = ddlCategory.Items[ddlCategory.SelectedIndex].Value.ToString();


            List<Catelogue> catelogueList = requestControl.getCatelogueItem(selectedCategory.Trim());
            bindCatelogueView(catelogueList);
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Department_Head/Approve.aspx");
        }

=======
       


        protected void add_Click(object sender, EventArgs e)
        {
            int rowIndex = 0;

            if (ViewState["SelectedItems"] != null)
            {
                DataTable table = (DataTable)ViewState["SelectedItems"];
                DataRow row = null;

                foreach (GridViewRow gridRow in CatelogueView.Rows)
                {
                    CheckBox chk = (CheckBox)gridRow.Cells[4].FindControl("chkChoose");

                    if (chk.Checked)
                    {
                        ((TextBox)gridRow.Cells[3].FindControl("txtQty")).Enabled = true;
                        string itemCode = ((HiddenField)gridRow.Cells[0].FindControl("itemCode")).Value;
                        string desc = gridRow.Cells[1].Text;
                        int qty = Convert.ToInt32(((TextBox)gridRow.Cells[3].FindControl("txtQty")).Text.Trim());

                        if (table.Rows.Count > 0)//if there're rows, to check selected items are previously selected too to update qty
                        {
                            bool isFound = false;
                            foreach (GridViewRow selectedItemRow in selectedItemView.Rows)
                            {
                                string selectedItemCode = ((HiddenField)selectedItemRow.Cells[0].FindControl("selectedItemCode")).Value.Trim();
                                if (itemCode == selectedItemCode)
                                {
                                    TextBox currentQtyBox = (TextBox)selectedItemRow.Cells[2].FindControl("selectedQty");
                                    int currentQty = Convert.ToInt32(currentQtyBox.Text.Trim());

                                    currentQtyBox.Text = (currentQty + qty).ToString();
                                    table.Rows[rowIndex]["qty"] = (currentQty + qty).ToString();
                                    ViewState["SelectedItems"] = table;

                                    isFound = true;
                                    break;
                                }
                                else
                                {
                                    ++rowIndex;
                                    isFound = false;
                                }
                            }
                            if (!isFound)
                            {
                                row = table.NewRow();
                                row["code"] = itemCode;
                                row["desc"] = desc;
                                row["qty"] = qty.ToString();

                                table.Rows.Add(row);
                            }

                        }
                        else//if no row, add new row
                        {
                            row = table.NewRow();
                            row["code"] = itemCode;
                            row["desc"] = desc;
                            row["qty"] = qty.ToString();

                            table.Rows.Add(row);
                        }
                    }
                    else
                    {
                        ((TextBox)gridRow.Cells[3].FindControl("txtQty")).Enabled = false;
                    }
                }
                selectedItemView.DataSource = table;
                selectedItemView.DataBind();
            }


            setDataToSelectedItemGridViewControls();
            btnSubmit.Enabled = true;
        }

        private void setDataToSelectedItemGridViewControls()
        {
            int rowIndex = 0;
            if (ViewState["SelectedItems"] != null)
            {
                DataTable table = (DataTable)ViewState["SelectedItems"];
                if (table.Rows.Count > 0)
                {
                    foreach(GridViewRow row in selectedItemView.Rows)
                    {
                        HiddenField newItemCode = (HiddenField)row.Cells[0].FindControl("selectedItemCode");
                        Label newItemDesc = (Label)row.Cells[1].FindControl("selectedItemDesc");
                        TextBox newItemQtyBox = (TextBox)row.Cells[2].FindControl("selectedQty");

                        newItemCode.Value = table.Rows[rowIndex]["code"].ToString();
                        newItemDesc.Text = table.Rows[rowIndex]["desc"].ToString();
                        newItemQtyBox.Text = table.Rows[rowIndex]["qty"].ToString();

                        ++rowIndex;
                    }
                }
            }
        }

        protected void removeLink_Click(object sender, EventArgs e)
        {
            LinkButton removeBtn = (LinkButton)sender;
            GridViewRow clickedRow = (GridViewRow)removeBtn.NamingContainer;

            int rowIndex = clickedRow.RowIndex;
            DataTable table = (DataTable)ViewState["SelectedItems"];

            table.Rows[rowIndex].Delete();
            ViewState["SelectedItems"] = table;

            selectedItemView.DataSource = table;
            selectedItemView.DataBind();

            setDataToSelectedItemGridViewControls();
        }

>>>>>>> .r28
    }
}